package com.example.loginpage.InputUtil

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.media.MediaScannerConnection
import android.os.Environment
import android.view.View
import android.widget.ScrollView
import java.io.File
import java.io.FileOutputStream
import java.io.IOException

class BitmapStorage {
    companion object{
        fun getPicture(context: Context, scrollView: ScrollView, fileName: String): Boolean {
            var height = 0
            for (i in 0 until scrollView.childCount) {
                val child = scrollView.getChildAt(i)
                child.measure(
                    View.MeasureSpec.makeMeasureSpec(scrollView.width, View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
                )
                height += child.measuredHeight
            }
            // Check if height is not zero before proceeding
            if (height > 0) {
                val bigBitmap = Bitmap.createBitmap(scrollView.width, height, Bitmap.Config.ARGB_8888)
                val bigCanvas = Canvas(bigBitmap)
                scrollView.draw(bigCanvas)

                // Save the bitmap to a file
                val picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                val file = File(picturesDirectory, fileName)

                try {
                    FileOutputStream(file).use { fos ->
                        bigBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)
                        fos.flush()
                        fos.close()
                    }
                    // Notify the media scanner about the new image
                    MediaScannerConnection.scanFile(context, arrayOf(file.path), null, null)
                    return true
                } catch (e: IOException) {
                    e.printStackTrace()
                    return false
                }
            } else {
                // Handle the case where height is zero, e.g., log an error or inform the user
                return false
                // You can also throw an exception or return a Result object indicating failure
            }
        }

        fun readPicture(fileName: String): Bitmap? {
            // Get the directory for the user's public pictures directory.
            val picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            val file = File(picturesDirectory, fileName)
            // Check if the file exists before attempting to read it
            if (file.exists()) {
                return BitmapFactory.decodeFile(file.absolutePath)
            }
            // If the file does not exist, return null
            return null
        }
        fun deletePicture(fileName: String): Boolean {
            // Get the directory for the user's public pictures directory.
            val picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            val file = File(picturesDirectory, fileName)
            // Check if the file exists before attempting to delete it
            if (file.exists()) {
                // Attempt to delete the file
                return file.delete()
            }
            // If the file does not exist, it's already "deleted"
            return true
        }
        fun renamePicture(currentFileName: String, newFileName: String): Boolean {
            // Get the directory for the user's public pictures directory.
            val picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            val currentFile = File(picturesDirectory, currentFileName)
            val newFile = File(picturesDirectory, newFileName)
            // Check if the current file exists before attempting to rename it
            if (currentFile.exists()) {
                // Attempt to rename the file
                return currentFile.renameTo(newFile)
            }
            // If the current file does not exist, return false
            return false
        }
    }
}